<template>
	<view class="">
		<u-toast ref="uToast" />
		<u-navbar :is-back="false" title="校园报修" back-icon-color='#333333' height="50" :border-bottom="false">
			<text class="iconfont icon-fanhuijian2" style="font-size:30rpx; margin-left:30rpx; font-weight:bold;"
				@click="previous_pages"></text>
			<view class="" style="margin-left: 600rpx; display: flex;align-content: center;"
				@click="$toLink('../add_maintenance/add_maintenance',{})">
				<image src="../../static/tupian96.png" mode="widthFix" style="width: 30rpx;"></image>
			</view>
		</u-navbar>
		<view class="top">
			<view class="topBox">
				<view v-for="(topItem,topIndex) of quanbu" :key="topIndex" :data-name="topIndex" @click="changeIndex"
					:class="[activeIndex==topIndex?'con':'']">
					<text></text>
					{{topItem}}
				</view>
			</view>
		</view>
		<view class="maintenance_list" v-for="(item,index) in repairList" :key="index"
			@click.stop="navigate_path(item)">
			<view class="per_conference">
				<view class="top">
					<text :style='{background:changeTag(item,2),color:changeTag(item,3)}'>{{changeTag(item,1)}}</text>
					<text>{{item.maintenance_content}}</text>
					<text>{{item.startTime}}</text>
				</view>
				<view class="center">
					<image src="../../static/tupian103.png" mode="widthFix"></image>
					<text>{{item.room_info?item.room_info[0].name:''}}</text>
				</view>
				<view class="bottom">
					<image src="../../static/tupian104.png" mode="widthFix"></image>
					<text>{{item.remark}}</text>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>
<script>
	import request from '../../request.js'
	import time from '../../time.js'
	export default {
		data() {
			return {
				quanbu: ['我的报修', '报修管理'],
				activeIndex: 0,
				back_page: 1,
				status: 'loading',
				page: 0,
				last_page: '',
				myHandleList: [],
				repair_list: [],
			}
		},
		onLoad(option) {
			if (option.id) {
				switch (option.id) {
					case 1:
						this.back_page = 1;
						break;
					case 2:
						this.back_page = 2;
						break;
					case 3:
						this.back_page = 3;
						break;
				}
			}
			this.repairLists();
		},
		computed: {
			repairList() {
				switch (this.activeIndex) {
					case 0:
						if (this.repair_list) {
							this.repair_list.forEach(item => {
								this.$set(item, 'startTime', time.format(item.create_time));
							})
							return this.repair_list;
						} else {
							return [];
						}
						break;
					case 1:
						if (this.myHandleList) {
							this.myHandleList.forEach(item => {
								this.$set(item, 'startTime', time.format(item.create_time));
							})
							return this.myHandleList;
						} else {
							return [];
						}
						break;
				}

			}
		},
		onReachBottom() { //滚动到底部触发该函数
			console.log('触发了吗')
			if (this.page >= this.last_page) {
				this.status = 'nomore';
				return
			} else {
				this.status = 'loading';
			} //如果大于最后一页那么就中断函数

			this.page = ++this.page;

			setTimeout(() => {
				switch (this.activeIndex) {
					case 0:
						this.repairLists(this.page);
						break;
					case 1:
						this.myHandle(this.page);
						break;
				}
			}, 2000)
		},

		methods: {
			changeIndex(e) {
				console.log(e.currentTarget.dataset.name);
				let index = e.currentTarget.dataset.name;
				this.activeIndex = index;
				switch (this.activeIndex) {
					case 0:
						//切换的时候让页数重新变为零,用于标识页面状态的值也要变为原始值
						this.page = 0;
						this.repairLists(1);
						break;
					default:
						//切换的时候让页数重新变为零,用于标识页面状态的值也要变为原始值
						this.page = 0;
						this.myHandle(1);
				}
			},
			navigate_path(param) {
				if (this.activeIndex == 0) {
					this.$toLink('../pending/pending', {
						id: param.repair_id
					});
				} else {
					this.$toLink('../accept_page/accept_page', {
						id: param.repair_id
					});
				}
			},
			changeTag(param, param2) {
				let str = '';
				if (param2 == 1) {
					switch (param.status) {
						case 1:
							str = '待处理';
							break;
						case 2:
							str = '同意';
							break;
						case 3:
							str = '拒绝';
							break;
					}
				} else if (param2 == 2) {
					switch (param.status) {
						case 1:
							str = '#FFA100';
							break;
						case 2:
							str = '#E5E5E5';
							break;
						case 3:
							str = '#EC654E';
							break;
					}
				} else {
					switch (param.status) {
						case 1:
							str = '#fff';
							break;
						case 2:
							str = '#666666';
							break;
						case 3:
							str = '#fff';
							break;
					}
				}
				return str;
			},
			//对返回上一级页面分情况跳转
			previous_pages() {
				switch (this.back_page) {
					case 1:
						this.$toLink('../career/career', {}, 3)
						break;
					case 2:
						uni.navigateBack({
							delta: 1
						})
						break;
					case 3:
						break;
				}
			},
			repairLists(param) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let pagination = param ? param : 1;
				let _this = this;
				request({
					url: base_url + 'teacher/repair/repairLists',
					data: {
						status: 0,
						page: pagination,
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							this.repair_list = [];
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.repair_list.length = 0;
								//没有数据的话那么就将数组清空
								_this.repair_list = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.repair_list = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.repair_list = [];
								} else {
									_this.repair_list = res.data.data.data;
								}
								console.log(_this.repair_list);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.repair_list = [..._this.repair_list, ...res.data.data.data];
								console.log(_this.repair_list);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.repair_list);

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			myHandle(param) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let pagination = param ? param : 1;
				let _this = this;
				request({
					url: base_url + 'teacher/repair/myHandle',
					data: {
						status: 0,
						page: pagination,
					},
					method: 'post',
					success(res) {
						if (res.statusCode == 200) {
							this.myHandleList = [];
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.myHandleList.length = 0;
								//没有数据的话那么就将数组清空
								_this.myHandleList = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.myHandleList = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.myHandleList = [];
								} else {
									_this.myHandleList = res.data.data.data;
								}
								console.log(_this.myHandleList);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.myHandleList = [..._this.myHandleList, ...res.data.data.data];
								console.log(_this.myHandleList);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.myHandleList);

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.top {
		width: 100%;
		height: 65rpx;
		background: #fff;
		overflow: hidden;

		.topBox {
			width: 100%;
			height: 65rpx;
			background: #fff;
			position: relative;
			display: flex;
			justify-content: space-evenly;
			border-bottom: 1px solid #F2F2F2;

			view {
				height: 65rpx;
				line-height: 65rpx;
				width: 120rpx;
				text-align: center;
				font-size: 30rpx;
				color: #fff;
				color: #333333;

				text {
					display: none;
				}


			}

			.con {
				position: relative;
				height: 65rpx;
				line-height: 65rpx;
				color: #2F96FE;
				width: 120rpx;

				text {
					display: block !important;
					position: absolute;
					width: 120rpx;
					height: 4rpx;
					background: #2F96FE;
					bottom: 0rpx;
					left: 0rpx;
				}
			}

		}
	}

	.list {
		width: 100%;
		height: 203rpx;
		overflow: hidden;

		.per_conference {
			margin-top: 22rpx;
			margin-left: 20rpx;
			height: 179rpx;
			width: 710rpx;
			border-radius: 15rpx;
			box-shadow: 0px 1px 1px 1px #ccc;

			.top {
				width: 100%;
				height: 60rpx;
				display: flex;
				justify-content: flex-start;

				text {
					margin-top: 20rpx;

					&:nth-child(1) {
						background-color: #FFA100;
						width: 100rpx;
						margin-left: 20rpx;
						height: 40rpx;
						border-radius: 10rpx;
						color: #fff;
						font-size: 15rpx;
						text-align: center;
						line-height: 40rpx;
					}

					&:nth-child(2) {
						color: #333333;
						width: 300rpx;
						text-align: right;
						font-size: 28rpx;
					}

					&:nth-child(3) {
						margin-left: 70rpx;
						color: #999999;
						font-size: 20rpx;
					}
				}

				.active {
					background: #EC654E !important;
				}
			}

			.center {
				width: 100%;
				height: 55rpx;
				display: flex;
				justify-content: flex-start;

				image {
					&:nth-child(1) {
						margin-top: 25rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}
				}

				text {
					&:nth-child(2) {
						margin-top: 25rpx;
						margin-left: 30rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}

			}

			.bottom {
				width: 100%;
				height: 45rpx;
				display: flex;
				justify-content: flex-start;

				image {
					&:nth-child(1) {
						margin-top: 15rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}
				}

				text {
					&:nth-child(2) {
						margin-top: 15rpx;
						margin-left: 30rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}
			}
		}
	}

	.maintenance_list {
		width: 100%;
		height: 203rpx;
		overflow: hidden;

		.per_conference {
			margin-top: 22rpx;
			margin-left: 20rpx;
			height: 179rpx;
			width: 710rpx;
			border-radius: 12rpx;
			box-shadow: 0px 1px 1px 1px #ccc;

			.top {
				width: 100%;
				height: 60rpx;
				display: flex;
				justify-content: flex-start;

				text {
					margin-top: 20rpx;

					&:nth-child(1) {
						background-color: #FFA100;
						width: 100rpx;
						margin-left: 20rpx;
						height: 40rpx;
						border-radius: 8rpx;
						color: #fff;
						font-size: 15rpx;
						text-align: center;
						line-height: 40rpx;
					}

					&:nth-child(2) {
						color: #333333;
						width: 290rpx;
						margin-left: 20rpx;
						text-align: right;
						font-size: 28rpx;
					}

					&:nth-child(3) {
						margin-left: 30rpx;
						color: #999999;
						font-size: 20rpx;

					}
				}

				.active {
					background: #EC654E !important;
				}
			}

			.center {
				width: 100%;
				height: 55rpx;
				display: flex;
				justify-content: flex-start;

				image {
					&:nth-child(1) {
						margin-top: 25rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}
				}

				text {
					&:nth-child(2) {
						margin-top: 25rpx;
						margin-left: 30rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}

			}

			.bottom {
				width: 100%;
				height: 45rpx;
				display: flex;
				justify-content: flex-start;

				image {
					&:nth-child(1) {
						margin-top: 15rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}
				}

				text {
					&:nth-child(2) {
						width: 600rpx;
						overflow: hidden;
						margin-top: 15rpx;
						margin-left: 30rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}
			}
		}
	}
</style>
